<template>
  <PageContainer title="基本资料">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="登录名称">
        <el-input v-model="form.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交修改</el-button>
      </el-form-item>
    </el-form>
  </PageContainer>
</template>
<script lang="ts" setup name="">
import { ref, reactive } from 'vue'
import PageContainer from '@/components/PageContainer.vue'
import { useUserStore } from '@/stores/user'
import { updateUserInfoService } from '@/api/user'

const userStore = useUserStore()

const formRef = ref()

//将用户数据解构出来  (只做渲染，无需响应式)
const { username, nickname, email, id } = userStore.user
// console.log(username, nickname, email, id)

//初始化用户数据
const form = reactive({
  username,
  nickname,
  email,
  id
})

const rules = {
  nickname: [
    { required: true, message: '请输入昵称', trigger: 'blur' },
    { min: 2, max: 10, message: '昵称长度在2-10个字符之间', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: ['blur', 'change'] }
  ]
}

const submitForm = async () => {
  //校验  等待校验结果
  await formRef.value.validate()
  //提交修改
  const res = await updateUserInfoService(form)
  console.log(res)
  //通知user模块数据更新  使用pinia中的getuser方法
  userStore.getUser()
  //提示
  ElMessage.success('修改成功')
}
</script>
<style scoped></style>
